package com.mxpio.erp.quality.controller;

import com.mxpio.erp.common.quality.entity.InspectionBillDetect;
import com.mxpio.erp.common.quality.service.InspectionBillDetectService;
import com.mxpioframework.common.vo.Result;
import com.mxpioframework.jpa.query.Criteria;
import com.mxpioframework.jpa.query.Operator;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.web.bind.annotation.*;

import java.io.UnsupportedEncodingException;
import java.util.List;

@Tag(name = "InspectionBillDetectController", description = "质检单据-检测项接口")
@RestController
@RequestMapping("/erp/quality/ibdetect/")
public class InspectionBillDetectController {

    @Autowired
    private InspectionBillDetectService service;

    @GetMapping("list")
    @Operation(summary = "质检单据-检测项列表", description = "获取质检单据-检测项列表（无分页）", method = "GET")
    public Result<List<InspectionBillDetect>> list(@Parameter(description="查询参数") Criteria criteria) throws UnsupportedEncodingException {
        List<InspectionBillDetect> inspectionBillDetect = service.list(InspectionBillDetect.class, criteria);
        return Result.OK(inspectionBillDetect);
    }

    @GetMapping("page")
    @Operation(summary = "质检单据-检测项列表", description = "获取质检单据-检测项列表（分页）", method = "GET")
    public Result<Page<InspectionBillDetect>> page(@Parameter(description="查询参数") Criteria criteria, @Parameter(description="每页条数")Integer pageSize, @Parameter(description="页号")Integer pageNo) throws UnsupportedEncodingException {
        Pageable page = PageRequest.of(pageNo-1, pageSize);
        Page<InspectionBillDetect> inspectionBillDetects = service.listPage(InspectionBillDetect.class, page, criteria);
        return Result.OK(inspectionBillDetects);
    }
    
    @GetMapping("list/{bizNo}")
    @Operation(summary = "根据bizNo获取质检单据-检测项", description = "根据bizNo获取质检单据-检测项", method = "GET")
    public Result<List<InspectionBillDetect>> getById(@Parameter(description="业务单号") @PathVariable(name = "bizNo", required = true) String bizNo) {
        Criteria c = Criteria.create().addCriterion("bizNo", Operator.EQ, bizNo);
        List<InspectionBillDetect> inspectionBillDetects = service.list(InspectionBillDetect.class, c);
        //List<InspectionBillDetect> inspectionBillDetects = service.listWith( c);//不用关联 inspectionItem对象
        service.computeCheckedSimples(bizNo,inspectionBillDetects);
        return Result.OK(inspectionBillDetects);
    }

    @DeleteMapping("remove/{bizNo}")
    @Operation(summary = "删除质检单据下的检测项", description = "删除质检单据下的检测项", method = "DELETE")
    public Result<InspectionBillDetect> remove(@PathVariable(name = "bizNo", required = true) String bizNo) {
        service.deleteByBizNo(bizNo);
        return Result.OK();
    }
}
